package com.portal.gui.salesman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.CustomerSale;
import com.portal.logic.CustomerSaleDAO;
import com.portal.logic.Sale;
import com.portal.logic.SaleDAO;

public class GetCustomersPerSaleScr extends JFrame {

	private JPanel contentPane;

	private static String selectedSale;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GetCustomersPerSaleScr frame = new GetCustomersPerSaleScr(selectedSale);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GetCustomersPerSaleScr(String selectedSale) {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		SaleDAO saleDAO = new SaleDAO(ConnectionManager.getConnection());
		Sale sale = saleDAO.selectByName(selectedSale);

		CustomerSaleDAO customerSaleDAO = new CustomerSaleDAO(ConnectionManager.getConnection());
		List<CustomerSale> customerSales = customerSaleDAO.selectAllCustomersPerSale(sale.getId());

		CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
		List<Customer> customers = new ArrayList<Customer>();
		Iterator<CustomerSale> iterator = customerSales.iterator();
		while (iterator.hasNext()) {
			CustomerSale customerSale = (CustomerSale) iterator.next();
			Customer customer = customerDAO.selectById(customerSale.getCustomerId());
			customers.add(customer);
		}


		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 179, 97, 25);
		contentPane.add(btnBack);




		final String[] values = new String[customers.size()];
		for (int i = 0; i < customers.size(); i++) {
			values[i] = customers.get(i).getName();
		}

		JList list = new JList();
		list.setModel(new AbstractListModel() {
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		list.setBounds(134, 23, 144, 65);
		contentPane.add(list);
	}

}

